//
//  ViewController.swift
//  xlogin
//
//  Created by bin hu on 2025/6/19.
//

import UIKit
import LocalAuthentication

class PasswordManager {
    static func currentPassword() -> String? {
        return UserDefaults.standard.string(forKey: "kPatterLockPassowrd")
    }

    static func updatePassword(_ password: String?) {
        UserDefaults.standard.setValue(password, forKey: "kPatterLockPassowrd")
    }
}

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    
    let titles = ["手势","指纹","面部"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.self))
        tableView.reloadData()
    }

}

extension ViewController {
    
    func gestureLogin() {
        self.navigationController?.pushViewController(EnterViewController(), animated: true)
    }
    
    func touchIdLogin() {
        faceLogin()
    }
    
    func faceLogin() {
        let context = LAContext()
        context.localizedCancelTitle = "取消"
        var authError: NSError?
        
        guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) else {
            print("设备不支持生物识别")
            return
        }
        
        switch context.biometryType {
        case .faceID: print("面容识别设备")//iPhone X 及更新机型
        case .touchID: print("指纹识别设备")//Phone 5s 至 iPhone 8 系列
        case .opticID: print("虹膜识别设备") // iOS 17+
        default: print("不支持生物识别")
        }
        
        context.evaluatePolicy(
            .deviceOwnerAuthenticationWithBiometrics,
            localizedReason: "解锁应用"
        ) { success, error in
            DispatchQueue.main.async {
                guard success, error == nil else {
                    print("验证失败: \(error?.localizedDescription ?? "")")
                    return
                }
                
                print("解锁成功！")
                // to do something
            }
        }
    }
    
}

extension ViewController:UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self))!
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let title = titles[indexPath.row]
        switch title {
        case "手势":
            gestureLogin()
        case "指纹":
            touchIdLogin()
        case "面部":
            faceLogin()
        default:
            break
        }
    }
}


